package com.fanrenqiji.modules.business.dict.web;

import com.fanrenqiji.common.config.GlobalSetting;
import com.fanrenqiji.common.persistence.Page;
import com.fanrenqiji.common.web.BaseController;
import com.fanrenqiji.modules.business.dict.entity.BusDict;
import com.fanrenqiji.modules.business.dict.service.BusDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年2月6日 下午2:14:36
 * @description 字典控制器类
 */
@Controller
@RequestMapping(value = "${adminPath}/business/busDict")
public class BusDictController extends BaseController {

	@Autowired
	private BusDictService busDictService;

	@ModelAttribute("busDict")
	public BusDict getBusDict(@RequestParam(required = false) Long id) {
		if (id != null) {
			return busDictService.findOne(id);
		}else {
			return new BusDict();
		}
	}

	/**
	 * 获得字典表的列表
	 * 
	 * @return
	 */
	@RequiresPermissions("business:busDict:view")
	@RequestMapping(value = { "list", "" })
	public String list(BusDict busDict, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		List<String> typeList = busDictService.findTypeList();
		model.addAttribute("typeList", typeList);
		Page<BusDict> page = busDictService.find(new Page<BusDict>(request, response),
				busDict);
		model.addAttribute("page", page);
		return "modules/business/base/busDictList";
	}

	/**
	 * 用户添加更新字典表
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("business:busDict:view")
	@RequestMapping(value = "form")
	public String form(Model model, BusDict busDict) {
		model.addAttribute("busDict", busDict);
		return "modules/business/base/busDictForm";
	}

	/**
	 * 添加或跟新数据字典
	 * 
	 * @param busDict
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("business:busDict:edit")
	@RequestMapping(value = "save")
	public String save(BusDict busDict, Model model,
			RedirectAttributes redirectAttributes) {// TODO @Valid 添加后端校验
		String errMsg="";
		if (!beanValidator(model, busDict)) {
			return form(model, busDict);
		}
			BusDict tempBus = busDictService.findBusDicsByTypeAndValue(busDict.getType(), busDict.getValue());
			if(tempBus!=null){
				if(busDict.getId()>0){
					busDictService.save(busDict);
					addMessage(redirectAttributes, "保存业务字典'" + busDict.getLabel() + "'成功");
				}else {
					errMsg = "该类型该键值已经添加";
					model.addAttribute("errMsg", errMsg);
					return form(model, busDict);
				}
			}else{
				busDictService.save(busDict);
				addMessage(redirectAttributes, "保存业务字典'" + busDict.getLabel() + "'成功");
			}
		return "redirect:" + GlobalSetting.getAdminPath()
				+ "/business/busDict/?repage&type=" + busDict.getType();
	}
	
	
	
	/**
	 * 通过类型或得字典值
	 * 
	 * @param type
	 *            类型
	 * @return
	 */
	@RequestMapping(value="getBusDicsByType",method= RequestMethod.GET )
	public @ResponseBody
    List<BusDict> getBusDicsByType(
			@RequestParam(value = "type") String type) {
		List<BusDict> busDicts = busDictService.findBusDicsByType(type);
		return busDicts;
	}
	
	
	/**
	 * 删除操作--更新关键属性状态
	 * @return
	 */
	@RequestMapping("delete")
	@RequiresPermissions("business:busDict:edit")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		busDictService.delete(id);
		addMessage(redirectAttributes, "业务字典删除成功");
		return "redirect:" + GlobalSetting.getAdminPath()
				+ "/business/busDict/";
	}

}
